﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Student
{
    public partial class Student : Form
    {
        public Student()
        {
            InitializeComponent();
            InitData();
            InitEvent();
        }

        private void InitEvent()
        {
            GridView.SelectionChanged += GridView_SelectionChanged;
        }

        private void InitData()
        {
            GridView.DataSource = dataTable;
        }
        DataTable dataTable
        {
            get
            {
                return SqlDataUtility.IniteGridData();
            }
            set { }
        }
        private string strName
        { get { return txtName.Text; } }
        private string NameNo
        { get { return txtNameNO.Text; } }
        private string Class
        { get { return txtClass.Text; } }
        private string College
        { get { return txtCollege.Text; } }
        private string Memo
        { get { return txtMemo.Text; } }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (HasSameNumber)
            {
                MessageBox.Show("重复的学号，请重新输入新学号！");
            }
            else
            {
                if (SqlDataUtility.Add(strName, NameNo, Class, College, Memo))
                {
                    MessageBox.Show("插入成功！");
                    DataRow row = dataTable.NewRow();

                    row[0] = txtName.Text;
                    row[1] = txtNameNO.Text;
                    row[2] = txtClass.Text;
                    row[3] = txtCollege.Text;
                    row[4] = txtMemo.Text;
                    dataTable.Rows.Add(row);
                }
                else
                {
                    MessageBox.Show("插入失败！");
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (HasSameNumber)
            {
                MessageBox.Show("重复的学号，请重新输入新学号！");
            }
            else
            {
                if (SqlDataUtility.Save(strName, NameNo, Class, College, Memo))
                {
                    MessageBox.Show("更新成功！");
                    DataRow row = dataTable.Rows[GridView.CurrentRow.Index];
                    row[0] = txtName.Text;
                    row[1] = txtNameNO.Text;
                    row[2] = txtClass.Text;
                    row[3] = txtCollege.Text;
                    row[4] = txtMemo.Text;
                }
                else
                {
                    MessageBox.Show("更新失败！");
                }
            }

        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (SqlDataUtility.Delete(NameNo))
            {
                MessageBox.Show("删除成功！");

                DataRow row = dataTable.Rows[GridView.CurrentRow.Index];
                dataTable.Rows.Remove(row);
            }
            else
            {
                MessageBox.Show("删除失败！请重试...");
            }
        }

        private void GridView_SelectionChanged(object sender, EventArgs e)
        {
            int index = GridView.CurrentRow.Index;
            if (index >= 0 && index < GridView.Rows.Count-1 )
            {
                DataRow row = dataTable.Rows[index];

                txtName.Text = row[0].ToString();
                txtNameNO.Text = row[1].ToString();
                txtClass.Text = row[2].ToString();
                txtCollege.Text = row[3].ToString();
                txtMemo.Text = row[4].ToString();
            }
        }

        private bool HasSameNumber
        {
            get
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    if (row[1].ToString().Contains(txtNameNO.Text.Trim()))
                    {
                        return true;
                    }
                }
                return false;
            }
        }
    }
}
